﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Configuration;
using TSL.Common.DEncrypt;
using TSL.Web.UI;
using System.Data;
using TSL.BLL;

namespace TSL.AjaxResult
{
    public class User : BasePage
    {
        [TSL.Utility.jQueryInvokeMethod()]
        public UserResult Check(string username, string password, string chkNumber)
        {
            if (string.IsNullOrEmpty(username))
            {
                return new UserResult(-1, "用户名不能为空！");
            }

            if (string.IsNullOrEmpty(password))
            {
                return new UserResult(-2, "密码不能为空！");
            }

            if (string.IsNullOrEmpty(chkNumber))
            {
                return new UserResult(-3, "验证码不能为空！");
            }

            if (chkNumber != HttpContext.Current.Request.Cookies["CheckCode"].Value)
            {
                return new UserResult(-4, "验证码错误！");
            }

            TSL.Model.SYS_users userInfo = new TSL.BLL.SYS_users().GetModelByName(username);
            if (userInfo == null)
            {
                return new UserResult(-5, "用户名不存在！");
            }

            if (TSL.Common.PassWord.deUserPW(password) != userInfo.userpass)
            {
                return new UserResult(-6, "用户密码错误！");
            }

            if ((Convert.ToInt32(userInfo.state) == 100) && ((userInfo.userclass == 0xbb9) || (userInfo.userclass == 0xbba)))
            {
                System.Web.Security.FormsAuthentication.SetAuthCookie(userInfo.username, false);

                userInfo.loginNum++;
                if (HttpContext.Current.Request.ServerVariables["HTTP_VIA "] != null)
                {
                    userInfo.LoginIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR "].ToString();
                }
                else
                {
                    userInfo.LoginIP = HttpContext.Current.Request.ServerVariables["Remote_Addr"].ToString();
                }
                new TSL.BLL.SYS_users().UpdateLogin(userInfo);
                new TSL.BLL.publish().AddUserLoginLog(userInfo.username, userInfo.LoginIP, HttpContext.Current.Request.Browser.Browser);
                DataTable table = new TSL.BLL.caipiao().GetHreadDateCache(userInfo.userclass);
                if (table.Rows.Count > 0)
                {
                    return new UserResult(PathNotLin() + table.Rows[0]["url"].ToString());
                }
                else
                {
                    return new UserResult(Path() + "lottery/index.aspx?id=100001");
                }
            }
            else
            {
                return new UserResult(-7, "该用户为非法用户！");
            }
        }

        [TSL.Utility.jQueryInvokeMethod()]
        public UserInfoResult GetUserInfo()
        {
            try
            {
                DataTable table = new TSL.BLL.GetUserInfo().GetListFromView("username='" + AsUser.username + "'").Tables[0];
                if (table.Rows.Count <= 0)
                {
                    return new UserInfoResult(-1, "未获取到用户数据");
                }

                UserInfoResult userinfor = new UserInfoResult();
                userinfor.Profit = table.Rows[0]["Profit"].ToString();
                userinfor.Usermoney = table.Rows[0]["Usermoney"].ToString();
                userinfor.Lockmoney = table.Rows[0]["Lockmoney"].ToString();
                return userinfor;

            }
            catch (Exception ex)
            {
                return new UserInfoResult(-1, "系统错误！");
            }
        }

        [TSL.Utility.jQueryInvokeMethod()]
        public UserInfoResult ChangeUserfdNums(string fdNums)
        {
            int IntfdNums = -1;
            try
            {
                IntfdNums = int.Parse(fdNums);
            }
            catch
            {
                return new UserInfoResult(-999, "非法入侵本次将会记录！");
            }


            try
            {
                SYS_users user = new SYS_users();
                user.ChangeFD(AsUser.username, IntfdNums);
                UserInfoResult userinfor = new UserInfoResult();
                return userinfor;
            }
            catch (Exception ex)
            {
                return new UserInfoResult(-1, "系统错误！");
            }
        }

        [TSL.Utility.jQueryInvokeMethod()]
        public OrderBuyResult createOrderBuy(string buyIssue, string cpGropID, string codes, string onemoney, string multiple,string allmoney)
        {
            int IntcpGropID = -1;
            try
            {
                IntcpGropID = int.Parse(cpGropID);
            }
            catch
            {
                return new OrderBuyResult(-999, "非法入侵本次将会记录！");
            }

            TSL.Model.Buy_SSC model = new TSL.Model.Buy_SSC();
            TSL.BLL.Buy_SSC dals = new TSL.BLL.Buy_SSC();

            model.username = AsUser.username;
            model.expect = buyIssue;
            model.codes = codes;
            model.allmoney = allmoney;
            model.onemoney = onemoney;
            model.beishu = multiple;
            model.protype = cpGropID.ToString();

            TSL.Model.caipiao caipiaos = new TSL.BLL.caipiao().GetModel(IntcpGropID);

            if (AsUser.uermoney < Convert.ToDecimal(model.allmoney))
            {
                return new OrderBuyResult(-1, "用户账户可用余额不足");
            }

            if (!BuyBetSSC.CheckCodes(model.codes))
            {
                return new OrderBuyResult(-2, "投注号码格式错误");
            }

            if (!BuyBetSSC.CheckTotalAmount(model.codes, model.allmoney, model.beishu))
            {
                return new OrderBuyResult(-3, "方案总金额不相符");
            }

            if (!BuyBetSSC.CheckBetMenu(model.protype))
            {
                return new OrderBuyResult(-4, "玩法不存在");
            }

            if ((model.expect == null) || (model.expect.Trim() == ""))
            {
                return new OrderBuyResult(-5, "期号不能为空");
            }

            if (!BuyBetSSC.CheckIssue(model.expect, model.protype))
            {
                return new OrderBuyResult(-6, "奖期非投注状态");
            }

            if (!BuyBetSSC.BuyCheckTotalAmount(model.allmoney, model.onemoney,model.beishu, model.protype))
            {
                return new OrderBuyResult(-7, "超过最大限住数");
            }

            model.codes = BuyBetSSC.ReCodes(model.codes);
            dals.AddBuy_SSC(model);
            return new OrderBuyResult();
        }

        [TSL.Utility.jQueryInvokeMethod()]
        public BuyHisResult GetBuyHisResult(string ssc_id)
        {
            int Intssc_id = -1;
            try
            {
                Intssc_id = int.Parse(ssc_id);
            }
            catch
            {
                return new BuyHisResult(-999, "非法入侵本次将会记录！");
            }

            DataTable table = new TSL.BLL.Buy_SSC().GetBuyHisAll(string.Concat(new object[] { " username='", AsUser.username, "' and sscid=", Intssc_id.ToString() })).Tables[0];
            if (table.Rows.Count == 0)
            {
                return new BuyHisResult(-2, "无数据！");
            }

            BuyHisResult openres = new BuyHisResult();
            openres.Table = table;
            return openres;
        }

        [TSL.Utility.jQueryInvokeMethod()]
        public fdnumResult GetfdNumResult()
        {
            if (AsUser == null)
            {
                return new fdnumResult(-1, "无数据！");
            }

            fdnumResult fr = new fdnumResult();

            fr.rate = AsUser.rate_dw;
            fr.fdnum = Convert.ToInt32(AsUser.fdnum);

            fr.MaxFD = Convert.ToDouble(WebConfig.MaxFD);
            fr.MinFD = Convert.ToDouble(WebConfig.MinFD);

            fr.MaxFJ = Convert.ToInt32(WebConfig.MaxFJ);
            fr.MinFJ = Convert.ToInt32(WebConfig.MinFJ);
            fr.FatFJ = Convert.ToInt32(WebConfig.FatFJ);

            fr.KeDu = Convert.ToDouble(WebConfig.kedu);
            fr.KeDuMax = Convert.ToInt32(WebConfig.kedumax);

            return fr;
        }

        [TSL.Utility.jQueryInvokeMethod()]
        public UserResult UserAddResult(string UserName, string UserClass, string UserRates, string UserPwd)
        {
            TSL.Model.SYS_users model = new TSL.BLL.SYS_users().GetModelByName(AsUser.username);
            double Rates = Convert.ToDouble(model.rate_dw);
            model.username = UserName;
            model.userpass = TSL.Common.PassWord.deUserPW(UserPwd);
            model.regfrom = "&" + AsUser.username + "&" + model.regfrom;
            model.rate_bdw = Convert.ToDouble(model.rate_dw.ToString()) - Convert.ToDouble(UserRates);
            model.rate_dw = Convert.ToDouble(UserRates);
            model.userclass = Convert.ToInt32(UserClass);

            bool flag = new TSL.BLL.SYS_users().Exists(model.username);

            if (string.IsNullOrEmpty(model.username))
            {
                return new UserResult(-1, "用户名不能为空！");
            }

            if (string.IsNullOrEmpty(model.userpass))
            {
                return new UserResult(-1, "用户密码不能为空！");
            }

            if (model.rate_dw > Rates)
            {
                return new UserResult(-1, "返点不能超过自己！");
            }

            if (flag)
            {
                return new UserResult(-1, "用户名已经存在！");
            }

            new TSL.BLL.SYS_users().QTAdd(model);

            return new UserResult();
        }

    }
}
